package leetcode100

// TODO 递归 【-】 从前序与中序遍历序列构造二叉树 
// TODO https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/solution/105-cong-qian-xu-yu-zhong-xu-bian-li-xu-zoqbz/

func buildTree(preorder []int, inorder []int) *TreeNode {
    if len(preorder) == 0 {
        return nil
    }
    root := &TreeNode{preorder[0], nil, nil}
    i := 0
    for ; i < len(inorder); i++ {
        if inorder[i] == preorder[0] {
            break
        }
    }
    root.Left = buildTree(preorder[1 : len(inorder[: i]) + 1], inorder[: i])
    root.Right = buildTree(preorder[len(inorder[: i]) + 1 :], inorder[i + 1 :])
    return root
}